//
//  JTPushTool.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/22.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#define NOTIF_LOC [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"notification.archive"]

#import "JTPushTool.h"

#import "JTUser.h"
#import "JTPassengerTrip.h"
#import "JTDriverTrip.h"

#import "JTConst.h"

#import <AVOSCloud.h>

@implementation JTPushTool

#warning when release , close [AVPush setProductionMode:NO] below

+ (void)pushToDriver:(JTDriverTrip *)driverTrip{
    
    AVQuery *pushQuery = [AVInstallation query];
    [pushQuery whereKey:@"UserInfo" equalTo:driverTrip.driver.objectId];
    
//    [AVPush setProductionMode:NO];
    AVPush *push = [[AVPush alloc] init];
    [push setQuery:pushQuery];
    
    NSString *str = [NSString stringWithFormat:@"%@ 想要搭乘您 %@ %@ 至 %@ 的车辆！", [JTUser currentUser].nickname, driverTrip.time, driverTrip.start, driverTrip.end];
    NSString *passTripId = [[NSUserDefaults standardUserDefaults] objectForKey:tmpPassTripID];
    
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          str, @"alert",
                          @"Increment", @"badge",
                          @"bingbong.aiff", @"sound",
                          @"乘客", @"identity",
                          passTripId, @"tripId",
                          [JTUser currentUser].objectId,@"userId",
                          nil];
    [push setData:data];
    [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            AVObject *theTrip = [AVObject objectWithoutDataWithClassName:@"DriverTrip" objectId:driverTrip.objectId];
            [theTrip addUniqueObject:passTripId forKey:@"Passenger"];
            [theTrip saveInBackground];
           
        }
    }];
}

+ (void)pushToDriver:(JTDriverTrip *)driverTrip withBlock:(pushCompletionBlock)result {
    AVQuery *pushQuery = [AVInstallation query];
    [pushQuery whereKey:@"UserInfo" equalTo:driverTrip.driver.objectId];
    
//    [AVPush setProductionMode:NO];
    AVPush *push = [[AVPush alloc] init];
    [push setQuery:pushQuery];
    
    NSString *str = [NSString stringWithFormat:@"%@ 想要搭乘您 %@ %@ 至 %@ 的车辆！", [JTUser currentUser].nickname, driverTrip.time, driverTrip.start, driverTrip.end];
    NSString *passTripId = [[NSUserDefaults standardUserDefaults] objectForKey:tmpPassTripID];
    NSLog(@"passTripID:%@", passTripId);
    
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          str, @"alert",
                          @"Increment", @"badge",
                          @"bingbong.aiff", @"sound",
                          @"乘客", @"identity",
                          passTripId, @"tripId",
                          [JTUser currentUser].objectId,@"userId",
                          nil];
    [push setData:data];
    [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
//            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//            [dict setObject:[JTUser currentUser].nickname forKey:@"passengerName"];
//            [dict setObject:[JTUser currentUser].mobilePhoneNumber forKey:@"phoneNumber"];
//            [dict setObject:driverTrip.time forKey:@"date"];
//            [dict setObject:driverTrip.start forKey:@"start"];
//            [dict setObject:driverTrip.end forKey:@"end"];
//            [AVOSCloud requestSmsCodeWithPhoneNumber:driverTrip.driver.mobilePhoneNumber templateName:@"passengerOrderToDriver" variables:dict callback:^(BOOL succeeded, NSError *error) {
//                if (succeeded) {
//                } else {
//                    NSLog(@"%@", error);
//                }
//            }];

        }
        result(succeeded, error);
    }];
}

+ (void)pushToRefusePassenger:(JTPassengerTrip *)passTrip withBlock:(pushCompletionBlock)result {
    AVQuery *pushQuery = [AVInstallation query];
    [pushQuery whereKey:@"UserInfo" equalTo:passTrip.passenger.objectId];
    
//    [AVPush setProductionMode:NO];
    AVPush *push = [[AVPush alloc] init];
    [push setQuery:pushQuery];
    
    NSString *str = [NSString stringWithFormat:@"司机 %@ 拒绝了您 %@ %@ 至 %@ 的搭乘请求！", [JTUser currentUser].nickname, passTrip.time, passTrip.start, passTrip.end];
    
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          str, @"alert",
                          @"Increment", @"badge",
                          @"bingbong.aiff", @"sound",
                          @"提醒",@"identity",
                          nil];
    
    [push setData:data];
    [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        result(succeeded, error);
    }];

}

+ (void)pushToPassenger:(JTPassengerTrip *)passTrip withBlock:(pushCompletionBlock)result {
    
    AVQuery *pushQuery = [AVInstallation query];
    [pushQuery whereKey:@"UserInfo" equalTo:passTrip.passenger.objectId];
    
//    [AVPush setProductionMode:NO];
    AVPush *push = [[AVPush alloc] init];
    [push setQuery:pushQuery];
    
    NSString *str = [NSString stringWithFormat:@"%@ 邀请您搭乘 %@ %@ 至 %@ 的车辆！", [JTUser currentUser].nickname, passTrip.time, passTrip.start, passTrip.end];
    NSString *driverTripId = [[NSUserDefaults standardUserDefaults] objectForKey:tmpDriverTripID];
    NSLog(@"driverTripId:%@", driverTripId);
    
    NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                          str, @"alert",
                          @"Increment", @"badge",
                          @"bingbong.aiff", @"sound",
                          @"司机",@"identity",
                          driverTripId, @"tripId",
                          [JTUser currentUser].objectId,@"userId",
                          nil];
    
    [push setData:data];
    [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
#warning 打开这一行会在通知搭乘时将自己列为乘客行程的司机
        if (succeeded) {
            NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
            [dict setObject:[JTUser currentUser].nickname forKey:@"nickName"];
//            [dict setObject:[JTUser currentUser].mobilePhoneNumber forKey:@"phoneNumber"];
//            [dict setObject:passTrip.time forKey:@"date"];
            [dict setObject:passTrip.start forKey:@"start"];
            [dict setObject:passTrip.end forKey:@"end"];
            [AVOSCloud requestSmsCodeWithPhoneNumber:passTrip.passenger.mobilePhoneNumber templateName:@"DriverToPassenger" variables:dict callback:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                } else {
                    NSLog(@"%@", error);
                }
            }];

        }
        result(succeeded, error);
    }];
}

+ (void)pushToRemindPassenger:(NSArray *)passTrips withBlock:(pushCompletionBlock)result {
    for (JTPassengerTrip *onePassTrip in passTrips) {
        AVQuery *pushQuery = [AVInstallation query];
        [pushQuery whereKey:@"UserInfo" equalTo:onePassTrip.passenger.objectId];
        
//        [AVPush setProductionMode:NO];
        AVPush *push = [[AVPush alloc] init];
        [push setQuery:pushQuery];
        
        NSString *str = [NSString stringWithFormat:@"司机 %@ 的 %@ %@ 至 %@ 的车辆已到达目的地！", [JTUser currentUser].nickname, onePassTrip.time, onePassTrip.start, onePassTrip.end];
        
        NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                              str, @"alert",
                              @"Increment", @"badge",
                              @"bingbong.aiff", @"sound",
                              @"提醒",@"identity",
                              nil];
        
        [push setData:data];
        [push sendPushInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
            result(succeeded, error);
        }];
    }
}

+ (NSMutableArray *)currentNotifications {
    NSMutableArray *notifs = [NSKeyedUnarchiver unarchiveObjectWithFile:NOTIF_LOC];
    return notifs;
}

+ (void)editNotifications:(NSMutableArray *)array {
    [NSKeyedArchiver archiveRootObject:array toFile:NOTIF_LOC];
}

@end
